﻿package morn.core.managers
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import morn.core.events.*;

    public class DragManager extends Object
    {
        private var _dragInitiator:DisplayObject;
        private var _dragImage:Sprite;
        private var _data:Object;

        public function DragManager()
        {
            return;
        }// end function

        public function doDrag(param1:Sprite, param2:Sprite = null, param3 = null, param4:Point = null) : void
        {
            var _loc_5:Point = null;
            this._dragInitiator = param1;
            this._dragImage = param2 ? (param2) : (param1);
            this._data = param3;
            if (this._dragImage != this._dragInitiator)
            {
                if (param4)
                {
                    this._dragImage.x = App.stage.mouseX - param4.x;
                    this._dragImage.y = App.stage.mouseY - param4.y;
                }
                else
                {
                    _loc_5 = this._dragInitiator.localToGlobal(new Point());
                    this._dragImage.x = _loc_5.x;
                    this._dragImage.y = _loc_5.y;
                }
                App.stage.addChild(this._dragImage);
            }
            this._dragImage.startDrag();
            this._dragInitiator.dispatchEvent(new DragEvent(DragEvent.DRAG_START, param1, param3));
            App.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onStageMouseMove);
            App.stage.addEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUp);
            return;
        }// end function

        private function onStageMouseMove(event:MouseEvent) : void
        {
            if (event.stageX <= 0 || event.stageX >= App.stage.stageWidth || event.stageY <= 0 || event.stageY >= App.stage.stageHeight)
            {
                this._dragImage.stopDrag();
            }
            else
            {
                this._dragImage.startDrag();
            }
            return;
        }// end function

        private function onStageMouseUp(event:Event) : void
        {
            App.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onStageMouseMove);
            App.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUp);
            this._dragImage.stopDrag();
            var _loc_2:* = this.getDropTarget(this._dragImage.dropTarget);
            if (_loc_2)
            {
                _loc_2.dispatchEvent(new DragEvent(DragEvent.DRAG_DROP, this._dragInitiator, this._data));
            }
            this._dragInitiator.dispatchEvent(new DragEvent(DragEvent.DRAG_COMPLETE, this._dragInitiator, this._data));
            if (this._dragInitiator != this._dragImage)
            {
                if (App.stage.contains(this._dragImage))
                {
                    App.stage.removeChild(this._dragImage);
                }
            }
            this._dragInitiator = null;
            this._data = null;
            this._dragImage = null;
            return;
        }// end function

        private function getDropTarget(param1:DisplayObject) : DisplayObject
        {
            while (param1)
            {
                
                if (param1.hasEventListener(DragEvent.DRAG_DROP))
                {
                    return param1;
                }
                param1 = param1.parent;
            }
            return null;
        }// end function

    }
}
